<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <%@include file="../_static/_head.jsp" %>
</head>
<body>
<div class="layui-card">
    <nav class="breadcrumb"><a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
    </nav>
    <div class="layui-card-header">定时器列表</div>
    <div class="layui-card-body">
        <script type="text/html" id="dataOperator">
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="update">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
            {{#  if(d.state == '-2'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">故障</button>
            {{#  } else if(d.state == '-1'){ }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="start">启动</a>
            {{#  } else if(d.state == '0'){ }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="end">停止</a>
            {{#  } else if(d.state == '1'){ }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="restart">运行</a>
            {{#  } else if(d.state == '2'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">完成</button>
            {{#  } else if(d.state == '3'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">错误</button>
            {{#  } else if(d.state == '4'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">阻塞</button>
            {{#  } }}
        </script>

        <div class="operator">
            <a class="layui-btn layui-btn-normal" href="javascript:void(0);" onclick="add('新增定时任务','/sys/quartz/add.html')" id="adda">新增任务</a>
        </div>

        <table id="dataTable" lay-filter="tabFilter"></table>

        <script type="text/html" id="stateTpl">
            {{#  if(d.state == '-2'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">故障</button>
            {{#  } else if(d.state == '-1'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">丢失</button>
            {{#  } else if(d.state == '0'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">正常</button>
            {{#  } else if(d.state == '1'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">暂停</button>
            {{#  } else if(d.state == '2'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">完成</button>
            {{#  } else if(d.state == '3'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">错误</button>
            {{#  } else if(d.state == '4'){ }}
            <button class="layui-btn layui-btn-warm layui-btn-radius layui-btn-xs">阻塞</button>
            {{#  } }}
        </script>

        <script type="text/html" id="createTimeTpl">
            {{#
            var date = new Date(d.createTime);
            return date.Format("yyyy-MM-dd hh:mm:ss");
            }}
        </script>
    </div>
</div>

<script src="<%=basePath%>plugins/layui/layui.js"></script>
<script src="<%=basePath%>plugins/dateformat.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'laydate'], function () {
        var form = layui.form, table = layui.table, laydate = layui.laydate,
            $ = layui.jquery;

        laydate.render({
            elem: '#sTime'
        });

        laydate.render({
            elem: '#eTime'
        });

        var tableIns = table.render({
            elem: '#dataTable'
            , id: 'dataTab'
            , url: '/sys/quartz/list.json'
            , page: true
            , cols: [[
                {field: 'id', title: 'ID', width:80, fixed: 'left'}
                ,{field: 'name', title: '任务名称'}
                ,{field: 'state', title: '状态',width:80,align:'center',templet: '#stateTpl'}
                ,{field: 'groupname', title: '分组名称'}
                ,{field: 'createTime', title: '创建时间',templet:'#createTimeTpl'}
                ,{field: 'cron', title: '表达式'}
                ,{field: 'description', title: '描述'}
                ,{fixed: 'right', title:'操作',width:200, align:'center', fixed: 'right',toolbar: '#dataOperator'}
            ]]
        });

        table.on('tool(tabFilter)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'update') { //查看
                var index = layer.open({
                    type: 2,
                    title: "修改任务信息",
                    content: "/sys/quartz/update.html?id=" + data.id
                });
                layer.full(index);
            } else if (layEvent === 'delete') { //删除
                layer.confirm('确定删除该行数据么', function (index) {
                    $.ajax({
                        url: "/sys/quartz/delete.json",
                        data: {id: data.id},
                        method: "POST",
                        success: function (data) {
                            if (data.code == 200) {
                                location.reload();
                            } else {
                                layer.msg(data.msg, {icon: 2, time: 3000});
                            }
                        }
                    });
                });
            } else if (layEvent === 'start') {
                layer.confirm('是否启动该定时任务', function (index) {
                    $.ajax({
                        url: "/sys/quartz/start.json",
                        data: {id: data.id},
                        method: "POST",
                        success: function (data) {
                            if (data.code == 200) {
                                location.reload();
                            } else {
                                layer.msg(data.msg, {icon: 2, time: 3000});
                            }
                        }
                    });
                });
            }else if (layEvent === 'end') {
                layer.confirm('是否停止该定时任务', function (index) {
                    $.ajax({
                        url: "/sys/quartz/end.json",
                        data: {id: data.id},
                        method: "POST",
                        success: function (data) {
                            if (data.code == 200) {
                                location.reload();
                            } else {
                                layer.msg(data.msg, {icon: 2, time: 3000});
                            }
                        }
                    });
                });
            }else if (layEvent === 'restart') {
                layer.confirm('是否重新运行该定时任务', function (index) {
                    $.ajax({
                        url: "/sys/quartz/restart.json",
                        data: {id: data.id},
                        method: "POST",
                        success: function (data) {
                            if (data.code == 200) {
                                location.reload();
                            } else {
                                layer.msg(data.msg, {icon: 2, time: 3000});
                            }
                        }
                    });
                });
            }
        });
    });

    function add(title, url) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    }
</script>
</body>
</html>
